package com.wwwarehouse.common.activity.media;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.view.PagerAdapter;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.wwwarehouse.common.R;
import com.wwwarehouse.common.activity.base.BaseActivity;
import com.wwwarehouse.common.activity.base.BaseApplication;
import com.wwwarehouse.common.bean.media.UploadType;
import com.wwwarehouse.common.constant.Constant;
import com.wwwarehouse.common.custom_widget.CustomViewPagerInternal;
import com.wwwarehouse.common.custom_widget.popupwindow.EasyPopupWindow;
import com.wwwarehouse.common.eventbus_event.UploadImageEvent;
import com.wwwarehouse.common.tools.PopUpUtils;
import com.wwwarehouse.common.tools.ThreadUtils;
import com.wwwarehouse.common.tripartite_widget.photoview.PhotoView;
import com.wwwarehouse.common.tripartite_widget.viewindicator.CirclePageIndicator;

import java.io.File;
import java.util.List;

import de.greenrobot.event.EventBus;

public class PreviewImageActivity extends BaseActivity implements EasyPopupWindow.ChildClickListener, View.OnClickListener {
	private CustomViewPagerInternal mCustomViewPagerInternal;
	private CirclePageIndicator mCirclePagerIndicator;
	private PagerAdapter mAdapter;

	private EasyPopupWindow mEasyPopupWindow;

	private List<UploadType> mSelectedUploadTypeList;
	private int mStartPosition;
	private String mPhotoViewPrefix = "file://";
	private boolean mIsForbidLongClick;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_preview_image);

		initView();
		initData();
	}

	private void initView() {
		mCustomViewPagerInternal = findView(R.id.view_pager);
		mCirclePagerIndicator = findView(R.id.page_indicator);
	}

	private void initData() {
		if (getIntent().getExtras() != null) {
			mSelectedUploadTypeList = (List<UploadType>) getIntent().getSerializableExtra(Constant.KEY_UPLOAD_LIST);
			mStartPosition = getIntent().getIntExtra(Constant.kEY_UPLOAD_POSITION, 0);

			String photoViewPrefix = getIntent().getStringExtra(Constant.KEY_PHOTO_VIEW_PREFIX);
			if (!TextUtils.isEmpty(photoViewPrefix)) {
				if (TextUtils.equals("http", photoViewPrefix)) {
					mPhotoViewPrefix = "";
				} else {
					mPhotoViewPrefix = photoViewPrefix;
				}
			}

			mIsForbidLongClick = getIntent().getBooleanExtra(Constant.KEY_FORBID_CLICK, false);
			// +
			mSelectedUploadTypeList.remove(mSelectedUploadTypeList.size() - 1);
		}

		if (mSelectedUploadTypeList != null) {
			mCustomViewPagerInternal.setAdapter(mAdapter = new PagerAdapter() {
				@Override
				public int getCount() {
					return mSelectedUploadTypeList.size();
				}

				@Override
				public View instantiateItem(ViewGroup container, int position) {
					PhotoView photoView = new PhotoView(container.getContext());
					UploadType uploadType = mSelectedUploadTypeList.get(position);
					BaseApplication.getApplicationInstance()
							.displayImg(mPhotoViewPrefix + uploadType.getPath(), photoView);
					container.addView(photoView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

					if (!mIsForbidLongClick) {
						photoView.setOnLongClickListener(new View.OnLongClickListener() {
							@Override
							public boolean onLongClick(View v) {
								mEasyPopupWindow = PopUpUtils.showPopUpBottom(R.layout.view_image_popupwindow_delete, PreviewImageActivity.this, findViewById(android.R.id.content), true, PreviewImageActivity.this);
								return true;
							}
						}); // 长压监听}
					}
					return photoView;
				}

				@Override
				public void destroyItem(ViewGroup container, int position, Object object) {
					container.removeView((View) object);
				}

				@Override
				public boolean isViewFromObject(View view, Object object) {
					return view == object;
				}

				public int getItemPosition(Object object) {
					return POSITION_NONE;
				}
			});
		}

		mCirclePagerIndicator.setViewPager(mCustomViewPagerInternal, mStartPosition);
	}

	@Override
	public void getChildView(int layoutId, View view) {
		Button delete = (Button) view.findViewById(R.id.btn_delete_photo);
		Button cancel = (Button) view.findViewById(R.id.btn_cancel);

		delete.setOnClickListener(this);
		cancel.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();

		if (id == R.id.btn_delete_photo) {
			int currentPosition = mCustomViewPagerInternal.getCurrentItem();
			final File file = new File(mSelectedUploadTypeList.get(currentPosition).getPath());

			ThreadUtils.newThread(new Runnable() {
				@Override
				public void run() {
					if (file != null) {
						//删除系统图库文件
						getContentResolver().delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, MediaStore.Images.Media.DATA + "=?", new String[]{file.getAbsolutePath()});

						//通知图库更新
						sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + file.getAbsolutePath())));

						//删除本地文件
						file.delete();
					}
				}
			});

			//更新ViewPager
			mSelectedUploadTypeList.remove(currentPosition);
			if (mSelectedUploadTypeList.size() > 0) {
				mAdapter.notifyDataSetChanged();
				mCirclePagerIndicator.notifyDataSetChanged();
			} else {
				finishActivity();
			}

			//同步更新GridView
			EventBus.getDefault().post(new UploadImageEvent(currentPosition + ""));
		} else if (id == R.id.btn_cancel) {

		}

		if (mEasyPopupWindow != null && mEasyPopupWindow.isShowing()) {
			mEasyPopupWindow.dismiss();
		}
	}
}
